package com.atguigui.leetcode;

import java.util.ArrayDeque;
import java.util.Queue;

/**
 * 1823.找出游戏的获胜者
 * Project: leetcode
 * Package: com.atguigui.leetcode
 * Version: 1.0
 * <p>
 * Created by  wangjiaxin  on 2022/5/4 21:17
 */
public class P1823FindTheWinnerOfTheCircularGame {
    public static void main(String[] args) {
        Solution solution = new P1823FindTheWinnerOfTheCircularGame().new Solution();
        // TO TEST
    }

    //leetcode submit region begin(Prohibit modification and deletion)
    class Solution {
        public int findTheWinner(int n, int k) {
            Queue<Integer> queue = new ArrayDeque<Integer>();
            for (int i = 1; i <= n; i++) {
                //放入队列中
                queue.offer(i);
            }

            while (queue.size() > 1) {
                for (int i = 1; i < k; i++) {
                    //从开头吐出一个并放入最后位置
                    queue.offer(queue.poll());
                }
                //永久吐出
                queue.poll();
            }

            return queue.poll();
        }
    }
}
